package com.warski.wonderfires.page.setting;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import com.warski.wonderfires.MainActivity;
import com.warski.wonderfires.R;
import com.warski.wonderfires.utils.LocaleUtils;
import java.util.Locale;
import static com.warski.wonderfires.utils.LocaleUtils.LOCALE_CHINESE;
import static com.warski.wonderfires.utils.LocaleUtils.LOCALE_ENGLISH;

/**
 * Created by xuhaiping on 2020/8/29.
 * 设置页面
 */
public class SettingFragment extends Fragment {

    private String[] localeList;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_setting, container, false);

        localeList = new String[2];
        localeList[0] = "中文";
        localeList[1] = "English";
        initMenuList(root);
        return root;
    }

    private void initMenuList(View root) {
        final ListView listView = root.findViewById(R.id.menulist);

        SettingMenuAdaopter adapter = new SettingMenuAdaopter(root.getContext());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0: //语言切换
                        int checkedItem;
                        final int[] whichChecked = new int[1];
                        Locale currentLocale = LocaleUtils.getCurrentLocale(getContext());

                        if (currentLocale.getLanguage().equals(String.valueOf(LOCALE_CHINESE))) checkedItem = 0;
                        else checkedItem = 1;
                        new AlertDialog.Builder(getContext())
                                .setTitle(R.string.setting_menu_language)
                                .setSingleChoiceItems(localeList, checkedItem, (dialog, which) -> {
                                    whichChecked[0] = which;
                                })
                                .setPositiveButton(R.string.switchover, (dialog, which) -> {
                                    if (whichChecked[0] == 1) {
                                        if (LocaleUtils.needUpdateLocale(getContext(), LOCALE_ENGLISH)) {
                                            LocaleUtils.updateLocale(getContext(), LOCALE_ENGLISH);
                                            restartAct();
                                        }
                                    } else {
                                        if (LocaleUtils.needUpdateLocale(getContext(), LOCALE_CHINESE)) {
                                            LocaleUtils.updateLocale(getContext(), LOCALE_CHINESE);
                                            restartAct();
                                        }
                                    }
                                    dialog.dismiss();

                                })
                                .setNegativeButton(R.string.cancel, (dialog, which) -> {
                                    dialog.dismiss();
                                })
                                .show();
                        break;
                    case 1: //操作说明
                        System.out.println("操作说明");
                        break;
                    case 2://关于我们
                        System.out.println("关于我们");
                        break;
                    case 3://帮助
                        System.out.println("帮助");
                        break;
                    default:
                        break;
                }
            }
        });
    }

    /**
     * 重启当前Activity
     */
    private void restartAct() {
        MainActivity.getInstance().restartAct();
    }
}
